
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑图书</title>
    <link rel="stylesheet" href="../../js/layui/css/layui.css">
    <script src="../../js/jquery-3.4.1.js"></script>
    <script src="../../js/layui/layui.all.js"></script>
    <style>
        .edit-container {
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .button-container {
            text-align: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
<%
    String bookId = request.getParameter("bookId");
%>
<div class="edit-container">
    <form class="layui-form" id="editBookForm">
        <input type="hidden" id="editBookId" name="book_id">
        <div class="layui-form-item">
            <label class="layui-form-label">书名</label>
            <div class="layui-input-block">
                <input type="text" id="editTitle" name="title" required lay-verify="required" placeholder="请输入书名"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">作者</label>
            <div class="layui-input-block">
                <input type="text" id="editAuthor" name="author" required lay-verify="required" placeholder="请输入作者"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">ISBN</label>
            <div class="layui-input-block">
                <input type="text" id="editIsbn" name="isbn" required lay-verify="required" placeholder="请输入ISBN"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">出版社</label>
            <div class="layui-input-block">
                <input type="text" id="editPublisher" name="publisher" required lay-verify="required"
                       placeholder="请输入出版社" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">出版日期</label>
            <div class="layui-input-block">
                <input type="date" id="editPublishDate" name="publish_date" required lay-verify="required"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">库存数量</label>
            <div class="layui-input-block">
                <input type="number" id="editStockQuantity" name="stock_quantity" required lay-verify="required"
                       placeholder="请输入库存数量" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <input type="text" id="editType" name="type" required lay-verify="required" placeholder="请输入类型"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">简介</label>
            <div class="layui-input-block">
                <textarea id="editIntroduce" name="introduce" required lay-verify="required" placeholder="请输入简介"
                          class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="button-container">
            <button class="layui-btn" id="saveEditBtn">保存</button>
            <button type="button" class="layui-btn layui-btn-primary" id="cancelBtn">取消</button>
        </div>
    </form>
</div>

<script>

    let bookId = <%= bookId %>;
    let bookData;
    let reviewList;
    let user;

    $(document).ready(function () {
        console.log("接收到的bookId:", bookId);

        $.ajax({
            url: '/bookDetailById',
            method: 'GET',
            data: {
                id: bookId
            },
            success: function (res) {
                bookData = res.data;
                loadBookDetail(bookData)
            },
            error: function () {
                console.log('服务器错误');
            }
        })


        /**
         * 填充书籍详情
         * @param editBook 信息数据
         */
        function loadBookDetail(editBook) {
            $('#editBookId').val(bookId);
            // 将数据填充到表单中
            $('#editTitle').val(editBook.title);
            $('#editAuthor').val(editBook.author);
            $('#editIsbn').val(editBook.isbn);
            $('#editPublisher').val(editBook.publisher);
            $('#editPublishDate').val(editBook.publish_date);
            $('#editStockQuantity').val(editBook.stock_quantity);
            $('#editType').val(editBook.type);
            $('#editIntroduce').val(editBook.introduce);

        }


        // 保存按钮事件
        $('#saveEditBtn').on('click', function (event) {
            event.preventDefault();
            if ($('#editBookForm')[0].checkValidity()) {
                // 表单验证通过
                $.ajax({
                    url: '/admin/updateBookById',
                    type: 'POST',
                    data: $('#editBookForm').serialize(),
                    success: function (response) {
                        // 根据响应处理
                        layui.layer.msg(response.msg, {icon: 1});
                        setTimeout(function () {
                            parent.document.getElementById('view_frame').src = "./page/admin/bookAdmin.jsp";
                        }, 1000);
                    },
                    error: function () {
                        alert('保存失败，请重试');
                    }
                });
            } else {
                // 表单验证不通过
                alert('请填写所有必填项');
            }
        });

        // 取消按钮事件
        $('#cancelBtn').on('click', function () {
            window.history.back(); // 返回上一页
        });


    });
</script>


</body>
</html>
